package com.qianchang.microfinance.view.mine.loan;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.dm.library.http.DMOKHttpUtils;
import com.dm.library.http.HttpParams;
import com.dm.library.http.base.JsonResult;
import com.dm.library.http.callback.RequestCallBack;
import com.dm.library.utils.ToastUtil;
import com.qianchang.microfinance.R;
import com.qianchang.microfinance.bean.MortgageGoodsBean;
import com.qianchang.microfinance.global.ConstantAppInfo;
import com.qianchang.microfinance.utils.DMConstant;
import com.qianchang.microfinance.view.base.BaseActivity;
import com.qianchang.microfinance.view.base.BaseRecyclerAdapter;
import com.qianchang.microfinance.view.base.EmptyLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 押品信息
 */
public class MortgageGoodsInfoActivity extends BaseActivity {

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.error_layout)
    EmptyLayout errorLayout;
    private String projectId;
    private BaseRecyclerAdapter<MortgageGoodsBean> mAdapter;
    private List<MortgageGoodsBean> mortgageGoodsBeanList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mortgage_goods_info);
        ButterKnife.bind(this);
        setTitleName(getString(R.string.Label_mortgage_goods_info));

        initView();
        initData();
    }

    @Override
    protected void initView() {
        super.initView();
        Bundle bundle = getBundle();
        if (null != bundle) {
            projectId = bundle.getString(ConstantAppInfo.PROJECT_ID);
        }
        mAdapter = new MortgageGoodsInfoAdapter(this);
        mAdapter.addAll(mortgageGoodsBeanList);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(mAdapter);
    }

    @Override
    protected void initData() {
        super.initData();
        HttpParams params = new HttpParams();
        params.put("projectId", projectId);//
        DMOKHttpUtils.getInstance().post(this, DMConstant.ApiURL.GET_MORTGAGE_LIST_DETAIL, params, new RequestCallBack<List<MortgageGoodsBean>>() {
            @Override
            public void onSuccess(JsonResult<List<MortgageGoodsBean>> response) {
                if (response.isReqDataSuccess()) {
                    List<MortgageGoodsBean> mortgageGoodsBeanList = response.getObject();
                    if (null != mortgageGoodsBeanList) {
                        mAdapter.clear();
                        mAdapter.addAll(mortgageGoodsBeanList);
                        mAdapter.notifyDataSetChanged();
                    }
                } else {
                    ToastUtil.getInstant().show(MortgageGoodsInfoActivity.this, response.getDescription());
                }
                isShowEmptyView();
            }

            @Override
            public void onFailure(String msg) {
                ToastUtil.getInstant().show(MortgageGoodsInfoActivity.this, msg);
            }

            @Override
            public void onConnectFailure(String msg) {

            }
        });
    }


    public void isShowEmptyView() {
        if (mAdapter.getItems().size() > 0) {
            errorLayout.setErrorType(EmptyLayout.HIDE_LAYOUT);
            recyclerView.setVisibility(View.VISIBLE);
            errorLayout.setVisibility(View.GONE);
        } else {
            recyclerView.setVisibility(View.GONE);
            errorLayout.setVisibility(View.VISIBLE);
            errorLayout.setErrorType(EmptyLayout.NO_DATA);
        }
    }


}
